package com.qlylkj.lbk.ui.home

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.forest.core.etx.startKtxActivity
import com.qlylkj.lbk.R
import com.qlylkj.lbk.app.addHost
import com.qlylkj.lbk.base.BaseVMFragment
import com.qlylkj.lbk.base.WebViewActivity
import com.qlylkj.lbk.model.bean.CoChainInfo
import com.qlylkj.lbk.model.bean.HomeViewType
import com.qlylkj.lbk.model.bean.News
import com.qlylkj.lbk.ui.company.CompanyInfoActivity
import com.qlylkj.lbk.ui.search.SearchActivity
import kotlinx.android.synthetic.main.fragment_home.*

class HomeFragment : BaseVMFragment<HomeViewModel>() {

    companion object {
        fun newInstance(): HomeFragment {
            return HomeFragment()
        }
    }

    private var articleViewList = mutableListOf<HomeViewType>()

    private val homeAdapter by lazy { HomeAdapter(articleViewList) }

    override fun providerVMClass() = HomeViewModel::class.java

    override fun setLayoutId() = R.layout.fragment_home

    override fun initView() {
        toSearchLayout.setOnClickListener(onClickListener)
        homeRefreshLayout.apply {
           setEnableLoadMore(false)
            setOnRefreshListener {
                getHomeData()
            }
        }
        homeRecyclerView.apply {
            layoutManager = LinearLayoutManager(activity)
            adapter = homeAdapter.apply {
                setOnItemClickListener { adapter, _, position ->
                    val homeViewType = adapter.getItem(position) as? HomeViewType
                    homeViewType?.apply {
                        when (adapter.getItemViewType(position)) {
                            HomeViewType.TYPE_NEWS -> {
                                val news = itemAny as? News
                                news?.apply {
                                    startKtxActivity<WebViewActivity>( values = mutableListOf(
                                        WebViewActivity.TITLE to title,
                                        WebViewActivity.URL to jumpUrl.addHost()))
                                }
                            }
                            HomeViewType.TYPE_CO_CHAIN_INFO -> {
                                val coChainInfo = itemAny as? CoChainInfo
                                coChainInfo?.apply {
                                    consumers.apply {
                                        startKtxActivity<CompanyInfoActivity>(values = mutableListOf(
                                        CompanyInfoActivity.COMPANY_ID to id,
                                        CompanyInfoActivity.COMPANY_NAME to companyName
                                    ))
                                    }
                                }
                            }
                            else -> {
                            }
                        }
                    }
                }
            }
        }
    }

    override fun initData() {
        getHomeData()
    }

    override fun startObserve() {
        super.startObserve()
        mViewModel.apply {
            uiState.observe(this@HomeFragment, Observer { uiModel ->
                uiModel?.run {
                    homeRefreshLayout.finishRefresh()
                    homeAdapter.replaceData(homeViewTypeList)
                }
            })
        }
    }

    private val onClickListener = View.OnClickListener {
        when (it.id) {
            R.id.toSearchLayout -> toSearch()
            else -> {
            }
        }
    }

    private fun toSearch() {
        startKtxActivity<SearchActivity>()
    }

    private fun getHomeData() {
        mViewModel.getHomeData()
    }

}